/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.huangjianbo.supos.openapi.core.tools;

import java.util.Collection;

/**
 * 字符串工具类
 * 
 * @author tomcat
 * @version 1.0
 */
public final class StringUtils {

    /**
     * 判断字符串是否为空
     * 
     * @param str 字符串
     * @return 是否为空, 为空返回true, 否则返回false
     */
    public static boolean isEmpty(String str) {
        return str == null || str.isEmpty();
    }

    /**
     * 判断字符串是否不为空
     * 
     * @param str 字符串
     * @return 是否不为空, 不为空返回true, 否则返回false
     */
    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    /**
     * 将集合用分隔符拼接成字符串
     * 
     * @param collection 集合，例如: [1,2,3]
     * @param separator 分隔符，例如: ,
     * @return 拼接后的字符串, 例如: 1,2,3
     */
    public static String join(Collection<?> collection, String separator) {
        if (collection == null || collection.isEmpty()) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        Object[] objects = collection.toArray();

        for (int i = 0; i < collection.size() - 1; i++) {
            sb.append(objects[i].toString()).append(separator);
        }

        if (!collection.isEmpty()) {
            sb.append(objects[collection.size() - 1].toString());
        }

        return sb.toString();
    }

    /**
     * 将集合用逗号拼接成字符串
     *
     * @param collection 集合
     * @return 拼接后的字符串, 例如: 1,2,3
     */
    public static String join(Collection<?> collection) {
        return join(collection, ",");   
    }
}
